package com.yongchao.role.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yongchao.base.BaseController;
import com.yongchao.role.service.Serviceimp.YYC_RoleServiceImp;
import com.yongchao.role.service.YYC_RoleService;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.BufferedReader;
import java.io.IOException;


@WebServlet(name="YYC_PostRoleController",urlPatterns="/yyc/add")
public class YYC_PostRoleController extends BaseController {

    private YYC_RoleService YYCRoleService =new YYC_RoleServiceImp();


    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //post方法获取请求代码如下
        BufferedReader reader = request.getReader();
        String read = reader.readLine();
        JSONObject object = JSON.parseObject(read);
        Object method = object.get("method");
        String name = object.getString("name");
        Integer id = object.getInteger("id");

        if ("addRole".equals(method)) {
            addRole(name,response);
        } else if ("deleteRoleById".equals(method)) {
            deleteRoleById(id,request,response);
        } else if ("updateRoleById".equals(method)) {
            updateRoleById(id,name,request,response);
        }
    }


    private  void addRole(String name,ServletResponse response) throws IOException {
        //将name的值传到Service 执行方法
        YYCRoleService.addRole(name);
        //返回前端
        printData(true,"",200,response);
    }

    private  void deleteRoleById(Integer id,ServletRequest request,ServletResponse response) throws IOException {


        YYCRoleService.deleteRoleById(id);

        printData(true,"",200,response);
    }

    private  void updateRoleById(Integer id,String name,ServletRequest request,ServletResponse response) throws IOException {

        YYCRoleService.updateRoleById(id,name);
        printData(true,"",200,response);
    }







}


